﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CILADA.Engine
{
    public class AssemblyAnalysedEventArgs : EventArgs
    {
        #region Properties
        public AssemblyAnalysisResult AnalysisResult { get; protected set; }
        public string Name { get; protected set; }
        public string Location { get; protected set; }
        public IDictionary<string, string> AssemblyProperties { get; protected set; }
        #endregion

        #region Constructor
        protected AssemblyAnalysedEventArgs()
        {
            this.AssemblyProperties = new Dictionary<string, string>();
        }

        public AssemblyAnalysedEventArgs(AssemblyAnalysisResult analysisResult, string assemblyLocation, IDictionary<string, string> assemblyProperties)
        {
            this.AnalysisResult = analysisResult;

            FileInfo assemLoc = new FileInfo(assemblyLocation);
            this.Name = assemLoc.Name;
            this.Location = assemLoc.FullName;

            if (null != assemblyProperties)
            {
                this.AssemblyProperties = assemblyProperties;
            }
            else
            {
                this.AssemblyProperties = new Dictionary<string, string>();
            }
        }
        #endregion

        #region Instance Methods
        public string GetStringForConsole()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("------- ANALYSING ASSEMBLY -------");
            sb.AppendLine("Location=" + this.Location);

            foreach (KeyValuePair<string, string> e in this.AssemblyProperties)
            {
                sb.AppendLine(e.Key+"="+e.Value);
            }

            string resultPrefix = "=> ";
            switch(this.AnalysisResult){
                case AssemblyAnalysisResult.AnalysisSuccessful:
                    sb.AppendLine(resultPrefix+"Analysis completed successfully.");
                    break;
                case AssemblyAnalysisResult.AssemblyNotFound:
                    sb.AppendLine(resultPrefix + "Error: Assembly was not found.");
                    break;
                case AssemblyAnalysisResult.ErrorDuringProcessing:
                    sb.AppendLine(resultPrefix + "Error: Problem during processing of assembly.");
                    break;
                case AssemblyAnalysisResult.NotDotNetAssembly:
                    sb.AppendLine(resultPrefix + "Error: this is not a .NET assembly");
                    break;
                case AssemblyAnalysisResult.Unknown:
                    sb.AppendLine(resultPrefix + "An unknown error occured during processing.");
                    break;
            }
            
            return sb.ToString();
        }
        #endregion
    }
}
